// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: yuanchenxi 836909158@qq.com
 * @Date: 2024-10-08 19:08:23
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-07 23:57:19
 * @FilePath: \SGFEM\Import\NastranBDF\src\Element\CQUADRParser.cpp
 */
#include "Import/BDFImport/BulkData/Element/CQUADRParser.h"

#include <iostream>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFImport.h"
#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
BULKDATA_ENTRY_PARSER_REG (CQUADR);
/*
// file://./../../Doc/MarkDown/BulkData/CQUADR/CQUADR.md>
*/
// 以下需要修改
enum CQUADRkey
{
    CQUADRKEY_EID         = 2,
    CQUADRKEY_PID         = 3,
    CQUADRKEY_G1          = 4,
    CQUADRKEY_G2          = 5,
    CQUADRKEY_G3          = 6,
    CQUADRKEY_G4          = 7,
    CQUADRKEY_THETAorMCID = 8,
    CQUADRKEY_ZOFFS       = 9,
    CQUADRKEY_TFLAG       = 13,
    CQUADRKEY_T1          = 14,
    CQUADRKEY_T2          = 15,
    CQUADRKEY_T3          = 16,
    CQUADRKEY_T4          = 17,
};
Common::Status BDF::BULKDATA::ParseCQUADR (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                           std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::CQUADRData tempCQUAD4;

    // TODO(bug 001) m_dominId的正确填写
    tempCQUAD4.m_domainId = 1;

    int curIDKey = 1;  // 当前字符串是第几项

    for (auto curStrKey : dividedEntryInfo)
    {
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        bool const isFloat = BDF::covertToScientificNotation (curStrKey);

        switch (curIDKey)
        {
        case CQUADRKEY_EID:
            tempCQUAD4.m_id  = convertToI64 (curStrKey);
            tempCQUAD4.m_pId = tempCQUAD4.m_id;
            break;
        case CQUADRKEY_PID:
            tempCQUAD4.m_pId = convertToI64 (curStrKey);
            break;
        case CQUADRKEY_G1:
            tempCQUAD4.m_g[0] = convertToI64 (curStrKey);
            break;
        case CQUADRKEY_G2:
            tempCQUAD4.m_g[1] = convertToI64 (curStrKey);
            break;
        case CQUADRKEY_G3:
            tempCQUAD4.m_g[2] = convertToI64 (curStrKey);
            break;
        case CQUADRKEY_G4:
            tempCQUAD4.m_g[3] = convertToI64 (curStrKey);
            break;
        case CQUADRKEY_THETAorMCID:
            if (!isFloat)
            {
                tempCQUAD4.m_mcId = convertToI64 (curStrKey);
            }
            else
            {
                tempCQUAD4.m_theta = convertToReal (curStrKey);
            }
            break;
        case CQUADRKEY_ZOFFS:
            tempCQUAD4.m_zoffs = convertToReal (curStrKey);
            break;
        case CQUADRKEY_T1:

            tempCQUAD4.m_ti[0] = convertToReal (curStrKey);
            break;
        case CQUADRKEY_T2:
            tempCQUAD4.m_ti[1] = convertToReal (curStrKey);
            break;
        case CQUADRKEY_T3:
            tempCQUAD4.m_ti[2] = convertToReal (curStrKey);
            break;
        case CQUADRKEY_T4:
            tempCQUAD4.m_ti[3] = convertToReal (curStrKey);
            break;
        case CQUADRKEY_TFLAG:
            tempCQUAD4.m_tflag = convertToI64 (curStrKey);
            if (1 == tempCQUAD4.m_tflag)
            {
                tempCQUAD4.m_ti[0] = tempCQUAD4.m_ti[1] = tempCQUAD4.m_ti[2] = tempCQUAD4.m_ti[3] = 1;
            }
            break;
        default:
            break;
        }
        curIDKey++;
    }

    return dbServiceFactory->get<IElementService> ()->Add (tempCQUAD4);
}
